home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2018 July / PCgo 07-2018 CD-ROM Germany.iso / nw.pak / Unnamed File 004903.txt < prev    next >
Encoding:
Text File  |  2015-07-29  |  3.9 KB  |  105 lines

  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. /**
  6.  * Custom bindings for the Serial API.
  7.  *
  8.  * The bindings are implemented by asynchronously delegating to the
  9.  * serial_service module. The functions that apply to a particular connection
  10.  * are delegated to the appropriate method on the Connection object specified by
  11.  * the ID parameter.
  12.  */
  13.  
  14. var binding = require('binding').Binding.create('serial');
  15. var context = requireNative('v8_context');
  16. var eventBindings = require('event_bindings');
  17. var utils = require('utils');
  18.  
  19. var serialServicePromise = function() {
  20.   // getBackgroundPage is not available in unit tests so fall back to the
  21.   // current page's serial_service module.
  22.   if (!chrome.runtime.getBackgroundPage)
  23.     return requireAsync('serial_service');
  24.  
  25.   // Load the serial_service module from the background page if one exists. This
  26.   // is necessary for serial connections created in one window to be usable
  27.   // after that window is closed. This is necessary because the Mojo async
  28.   // waiter only functions while the v8 context remains.
  29.   return utils.promise(chrome.runtime.getBackgroundPage).then(function(bgPage) {
  30.     return context.GetModuleSystem(bgPage).requireAsync('serial_service');
  31.   }).catch(function() {
  32.     return requireAsync('serial_service');
  33.   });
  34. }();
  35.  
  36. function forwardToConnection(methodName) {
  37.   return function(connectionId) {
  38.     var args = $Array.slice(arguments, 1);
  39.     return serialServicePromise.then(function(serialService) {
  40.       return serialService.getConnection(connectionId);
  41.     }).then(function(connection) {
  42.       return $Function.apply(connection[methodName], connection, args);
  43.     });
  44.   };
  45. }
  46.  
  47. binding.registerCustomHook(function(bindingsAPI) {
  48.   var apiFunctions = bindingsAPI.apiFunctions;
  49.   apiFunctions.setHandleRequestWithPromise('getDevices', function() {
  50.     return serialServicePromise.then(function(serialService) {
  51.       return serialService.getDevices();
  52.     });
  53.   });
  54.  
  55.   apiFunctions.setHandleRequestWithPromise('connect', function(path, options) {
  56.     return serialServicePromise.then(function(serialService) {
  57.       return serialService.createConnection(path, options);
  58.     }).then(function(result) {
  59.       var id = result.info.connectionId;
  60.       result.connection.onData = function(data) {
  61.         eventBindings.dispatchEvent(
  62.             'serial.onReceive', [{connectionId: id, data: data}]);
  63.       };
  64.       result.connection.onError = function(error) {
  65.         eventBindings.dispatchEvent(
  66.             'serial.onReceiveError', [{connectionId: id, error: error}]);
  67.       };
  68.       return result.info;
  69.     }).catch (function(e) {
  70.       throw new Error('Failed to connect to the port.');
  71.     });
  72.   });
  73.  
  74.   apiFunctions.setHandleRequestWithPromise(
  75.       'disconnect', forwardToConnection('close'));
  76.   apiFunctions.setHandleRequestWithPromise(
  77.       'getInfo', forwardToConnection('getInfo'));
  78.   apiFunctions.setHandleRequestWithPromise(
  79.       'update', forwardToConnection('setOptions'));
  80.   apiFunctions.setHandleRequestWithPromise(
  81.       'getControlSignals', forwardToConnection('getControlSignals'));
  82.   apiFunctions.setHandleRequestWithPromise(
  83.       'setControlSignals', forwardToConnection('setControlSignals'));
  84.   apiFunctions.setHandleRequestWithPromise(
  85.       'flush', forwardToConnection('flush'));
  86.   apiFunctions.setHandleRequestWithPromise(
  87.       'setPaused', forwardToConnection('setPaused'));
  88.   apiFunctions.setHandleRequestWithPromise(
  89.       'send', forwardToConnection('send'));
  90.  
  91.   apiFunctions.setHandleRequestWithPromise('getConnections', function() {
  92.     return serialServicePromise.then(function(serialService) {
  93.       return serialService.getConnections();
  94.     }).then(function(connections) {
  95.       var promises = [];
  96.       for (var connection of connections.values()) {
  97.         promises.push(connection.getInfo());
  98.       }
  99.       return Promise.all(promises);
  100.     });
  101.   });
  102. });
  103.  
  104. exports.binding = binding.generate();
  105.